{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Conditionals\n",
    "\n",
    "#### with the `if` keyword\n",
    "In Julia, the syntax\n",
    "\n",
    "```julia\n",
    "if *condition 1*\n",
    "    *option 1*\n",
    "elseif *condition 2*\n",
    "    *option 2*\n",
    "else\n",
    "    *option 3*\n",
    "end\n",
    "```\n",
    "\n",
    "allows us to conditionally evaluate one of our options.\n",
    "<br><br>\n",
    "For example, we might want to implement the FizzBuzz test: given a number, N, print \"Fizz\" if N is divisible by 3, \"Buzz\" if N is divisible by 5, and \"FizzBuzz\" if N is divisible by 3 and 5. Otherwise just print the number itself! Enter your choice for `N` here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if (N % 3 == 0) && (N % 5 == 0) # `&&` means \"AND\"; % computes the remainder after division\n",
    "    println(\"FizzBuzz\")\n",
    "elseif N % 3 == 0\n",
    "    println(\"Fizz\")\n",
    "elseif N % 5 == 0\n",
    "    println(\"Buzz\")\n",
    "else\n",
    "    println(N)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### with ternary operators\n",
    "\n",
    "For this last block, we could instead use the ternary operator with the syntax\n",
    "\n",
    "```julia\n",
    "a ? b : c\n",
    "```\n",
    "\n",
    "which equates to \n",
    "\n",
    "```julia\n",
    "if a\n",
    "    b\n",
    "else\n",
    "    c\n",
    "end\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's say we want to return the larger of two numbers. Give `x` and `y` values here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x =\n",
    "y ="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the `if` and `else` keywords, we might write:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if x > y\n",
    "    x\n",
    "else\n",
    "    y\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and as a ternary operator, the conditional looks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x > y) ? x : y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### with short-circuit evaluation\n",
    "\n",
    "We've already seen expressions with the syntax\n",
    "```julia\n",
    "a && b\n",
    "```\n",
    "to return true if both `a` and `b` are true. Of course, if `a` is false, Julia doesn't even need to know the value of `b` in order to determine that the overall result will be false. So Julia doesn't even need to check what `b` is; it can just \"short-circuit\" and immediately return `false`.  The second argument `b` might be a more complicated expression like a function call with a side-effect, in which case it won't even be called:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "false && (println(\"hi\"); true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "true && (println(\"hi\"); true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On the other hand, if `a` is true, Julia knows it can just return the value of `b` as the overall expression. This means that `b` doesn't necessarily need evaluate to `true` or `false`!  `b` could even be an error:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x > 0) && error(\"x cannot be greater than 0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, check out the `||` operator, which also uses short-circuit evaluation to perform the \"or\" operation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "true || println(\"hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "false || println(\"hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Exercises\n",
    "\n",
    "#### 5.1 \n",
    "Write a conditional statement that prints a number if the number is even and the string \"odd\" if the number is odd."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2 \n",
    "Rewrite the code from 5.1 using a ternary operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.0",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
